function setNodeAttribute(node, attribute, value)
{
	if(attribute == "class")
		node.className = value;
	else if(attribute == "checked")
		node.defaultChecked = value;
	else if(attriubute == "for")
		node.htmlFor = value;
	else if(attribute == "style")
		node.style.cssText = value;
	else
		node.setAttribute(attribute, value);		
}

function dom(name, attributes){
	var node = document.createElement(name);
	if(attributes){
		forEachIn(attribute, function(name, value){
			setNodeAttribute(node, name, value);
		});
	}
	for(var i = 2; i<arguments.length; i++)
	{
		var child = argument[i];
		if(typeof child == "string")
			child = document.createTextNode(child);
		node.appendChild(child);
	}
	return node;
}

function forEachIn(object, action)
{
	for(var property in object){
		if(object.hasOwnProperty(property)){
			action(property, object[property]);
		}
	}
}
	


var newImage = document.createElement("IMG");
newImage.setAttribute("src", "img/Hiva.png");
document.body.appendChild(newImage);
document.write(newImage.getAttribute("src"));
document.write("<br>");

newImage.style.width = "400px";
newImage.style.height = "200px";

document.write("picture offset: ");
document.write(newImage.offsetWidth);
document.write("<br>");

//create head and text node
var secondHeader = document.createElement("H1");
var secondTitle = document.createTextNode("Chapter 2: Deep magic");
secondHeader.appendChild(secondTitle);
document.body.appendChild(secondHeader);

function $(id){
	return document.getElementById(id);
}


function isTextNode(node){
	return node.nodeType == 3;
}


document.write(isTextNode(document.body));
document.write("<br>");

document.write(document.firstChild.nodeName);
document.write("<br>");


